package me.zhengjie.hr.employeebase.repository;

import me.zhengjie.hr.employeebase.domain.EmployeeBase;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
//import org.apache.ibatis.annotations.Param;
import java.sql.Date;
import org.springframework.data.repository.query.Param;
import java.sql.Timestamp;
import java.util.List;

/**
* @author ZGZ
* @date 2020-04-15
*/
public interface EmployeeBaseRepository extends JpaRepository<EmployeeBase, Integer>, JpaSpecificationExecutor<EmployeeBase> {
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query(value = "update employee_base set center_belong = ?2 where id = ?1",nativeQuery = true)
    void updateChange(Integer id, Integer center_belong );


    @Query(value = "select * from employee_base where leave_time is not null",nativeQuery = true)
    Page<EmployeeBase> getLeave(Pageable pageable);

    @Query(value = "select * from employee_base where leave_time is null",nativeQuery = true)
    Page<EmployeeBase> getOnjob(Pageable pageable);

    @Query(value = "select * from employee_base where emp_name =?1 and center_belong=?2",nativeQuery = true)
    Integer getId(String name, Integer centerId);

    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query(value = "update employee_base set  leave_time = ?2 , leave_reason = ?3 , leave_inf = ?4 ,is_leave = 1 where id = ?1",nativeQuery = true)
    void updateLeave(Integer id, Date leaveTime, String leaveReason, String leaveInf);

    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query(value = "update employee_base set leave_time = null, leave_reason = null , leave_inf = null ,is_leave = 0 where id = ?1",nativeQuery = true)
    void updateOnjob(Integer id);

    @Query(value = "select leave_time from employee_base where id = ?1",nativeQuery = true)
    Date TimeisNull(Integer id);

    @Query(value = "select * from employee_base where `join` >= ?1 and `join` <= ?2",
            countQuery = "select count(*) from employee_base where `join` >= ?1 and `join` <= ?2",
            nativeQuery = true)
    Page<EmployeeBase> getTimeRange(String timeStart, String timeEnd,Pageable pageable);


    @Query(value = "select * from employee_base where  = ?2 ",
            countQuery = "select count(*) from employee_base where ?1 = ?2",
            nativeQuery = true)
    Page<EmployeeBase> getLizhiSearch(@Param("Type")  String Type, @Param("Value")  String Value,Pageable pageable);



}